import time

import pytest
from selenium.webdriver.common.by import By

from dxm.data import read_excel, db_select
from dxm.operation import Operation


class TestCase(Operation):
    @pytest.fixture(scope='function', autouse=True)
    def fixture(self):
        driver = self.driver()
        yield driver

    @pytest.mark.parametrize('username,password,expect', read_excel('login_success', 'login'))
    def test_login_success(self, fixture, username, password, expect):
        Operation.login(self, fixture, username, password)
        time.sleep(1)
        result = fixture.title
        assert result == expect

    @pytest.mark.parametrize('username,password,expect', read_excel('login_failed', 'login'))
    def test_login_failed(self, fixture, username, password, expect):
        Operation.login(self, fixture, username, password)
        help_block = fixture.find_elements(By.XPATH, "//*[@class='help-block']")
        result = []
        for i in help_block:
            result.append(i.text)
        assert expect in result

    @pytest.mark.parametrize('username,password,repassword,expect1', read_excel('signup_success', 'signup'))
    def test_signup_success(self, fixture, username, password, repassword, expect1):
        Operation.signup(self, fixture, username, password, repassword)
        time.sleep(1)
        result1 = fixture.current_url
        result2 = db_select(f"select count(*)from user where username='{username}'")[0][0]
        expect2 = 1
        assert (result1 == expect1) and (result2 == expect2)

    @pytest.mark.parametrize('username,password,repassword,expect', read_excel('signup_failed', 'signup'))
    def test_signup_failed(self, fixture, username, password, repassword, expect):
        Operation.signup(self, fixture, username, password, repassword)
        help_block = fixture.find_elements(By.XPATH, "//*[@class='help-block']")
        result = []
        for i in help_block:
            result.append(i.text)
        assert expect in result

    @pytest.mark.parametrize('username,password,bankcard_name, bankcard_type,bankcard_num',
                             read_excel('bankcard_add', 'bankcard'))
    def test_bankcard_add(self, fixture, username, password, bankcard_name, bankcard_type, bankcard_num):
        Operation.login(self, fixture, username, password)
        expect1 = Operation.bankcard_count(self, fixture) + 1
        Operation.bankcard_add(self, fixture, bankcard_name, bankcard_type, bankcard_num)
        result1 = Operation.bankcard_count(self, fixture)
        result2 = db_select(f"select count(*)from bankcard where cardNum ='{bankcard_num}' and userId=1;")[0][0]
        expect2 = 1
        assert (result1 == expect1) and (result2 == expect2)

    @pytest.mark.parametrize('username,password,bankcard_name, bankcard_type, bankcard_num',
                             read_excel('bankcard_del', 'bankcard'))
    def test_bankcard_del(self, fixture, username, password, bankcard_name, bankcard_type, bankcard_num):
        Operation.login(self, fixture, username, password)
        expect = Operation.bankcard_count(self, fixture) - 1
        Operation.bankcard_del(self, fixture)
        result = Operation.bankcard_count(self, fixture)
        assert result == expect

    @pytest.mark.parametrize('username,password,bankcard_name, bankcard_type,bankcard_num',
                             read_excel('bankcard_update', 'bankcard'))
    def test_bankcard_update(self, fixture, username, password, bankcard_name, bankcard_type, bankcard_num):
        Operation.login(self, fixture, username, password)
        Operation.bankcard_update(self, fixture, bankcard_name, bankcard_type, bankcard_num)
        expect = [bankcard_name, bankcard_type, bankcard_num]
        result = Operation.bankcard_search(self, fixture)
        assert result == expect

    @pytest.mark.parametrize('username,password,fund_type,pay_password,expect', read_excel('fund', 'fund'))
    def test_fund(self, fixture, username, password, fund_type, pay_password, expect):
        Operation.login(self, fixture, username, password)
        result = Operation.fund(self, fixture, fund_type, pay_password)
        assert result == expect

    @pytest.mark.parametrize('username,password,money,term,expect', read_excel('loan_success', 'loan'))
    def test_loan_success(self, fixture, username, password, money, term, expect):
        Operation.login(self, fixture, username, password)
        expect = Operation.loan_count(self, fixture) + 1
        Operation.loan(self, fixture, money, term)
        result = Operation.loan_count(self, fixture)
        assert result == expect

    @pytest.mark.parametrize('username,password,money,term,expect', read_excel('loan_approved', 'loan'))
    def test_loan_approved(self, fixture, username, password, money, term, expect):
        Operation.login(self, fixture, username, password)
        expect = Operation.bankcard_money(self, fixture) + int(money)
        Operation.loan(self, fixture, money, term)
        time.sleep(1)
        fixture.get('http://localhost:90')
        time.sleep(1)
        Operation.login(self, fixture, 'admin', '123456')
        Operation.loan_approve(self, fixture)
        time.sleep(2)
        fixture.get('http://localhost:90')
        time.sleep(1)
        Operation.login(self, fixture, username, password)
        result = Operation.bankcard_money(self, fixture)
        assert result == expect

    @pytest.mark.parametrize('username,password,money,term,expect', read_excel('loan_failed', 'loan'))
    def test_loan_failed(self, fixture, username, password, money, term, expect):
        Operation.login(self, fixture, username, password)
        Operation.loan(self, fixture, money, term)
        result = fixture.find_element(By.XPATH,
                                      "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect
